Mixes up the order of the places in target, in place: afterwards it holds the same values in a random order. It makes no copy. Any kind of value can be shuffled, text included.
array_shuffle(target)| Name | Type |
|---|---|
target | array |
Nothing.
Mixes up the order of the places in target, an array. Afterwards the array holds exactly the same values as before, in a random order.
array_shuffle(order);array_shuffle moves the values that are already in target. It does not make a new array, and it needs no extra memory. The old order is gone afterwards. If you want to keep it, make a copy with array_copy first and shuffle the one you want mixed up.
It mixes the places the array really has, the number array_length gives you. An array never gains or loses a place, so it has the same places afterwards.
Any kind of value can be shuffled: numbers, text, structs, references. Nothing is compared, so there is no rule about what the array holds. (A sort is different: it only works on numbers and text.)
A shuffle picks its random numbers from the same place roll_int does: one stream of random numbers for the whole game. There is no seed to give the shuffle itself. What follows from that:
roll_reseed changes it. Calling roll_reseed with a number starts the stream again from that number, so roll_reseed(7) just before array_shuffle gives the same shuffle every time you do it with the array in the same order to start with. Use that when you want a shuffle you can repeat. For a different shuffle each game, reseed from something the player decides, such as how long they waited before pressing a button (see roll_reseed).roll_int, roll_real and roll_chance that comes after it.Every possible order can come up. It is very slightly more likely to land on some orders than others, because a random number is cut down to fit a small range, but the difference is far too small for a game to show.
| You do this | What happens |
|---|---|
| shuffle an array with one place | nothing changes, and no random numbers are used |
| shuffle an array that has the same value more than once | works as normal. Equal values are interchangeable, so you cannot tell which is which |
| shuffle and hope a value moves | it may stay where it was. That can happen with random order, and it gets less likely the more places there are |
| give it a list or a grid | refused when checked, and the message names the function that shuffles that kind, like list_shuffle |
| use it where a value is needed | refused when checked. It gives nothing back |
A script that takes an array as a parameter works on the caller's own array (see Arrays). So array_shuffle inside a script shuffles the caller's array, whatever its size.
Nothing is set aside: the values are swapped where they stand, one swap for each place after the first. No timing was measured.
var order = array_create(4, 0);
order[0] = 1;
order[1] = 2;
order[2] = 3;
order[3] = 4;
array_shuffle(order); // the same four numbers, in a new order
var first = order[0]; // whichever one landed firstShuffles the array_length(target) places the array has. A Fisher-Yates shuffle done in place: it makes no copy and needs no extra memory. For each place from the last down to the second, it swaps that place with one picked at random from the places up to and including it, so every order can come up. The pick comes from gbml::roll_int, the one stream the roll_ functions use, so a shuffle follows the same rules: the same ROM shuffles the same way every time it is switched on, roll_reseed changes that, and roll_reseed with the same number just before a shuffle gives the same shuffle again. A shuffle uses up rolls from that stream (one for each place after the first), so it moves every later roll along. Nothing is compared, so any kind of item can be shuffled.